package com.c6;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/cyrServet")
public class ServletConfigTest extends HttpServlet {
    private ServletConfig config;
    public ServletConfigTest() {
        super();
    }
    @Override
    public void destroy() {
        super.destroy();
    }
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.config = config;
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        String charset=this.config.getInitParameter("charset");
        System.out.println(charset);
        response.setCharacterEncoding(charset);    //设置响应的字符集格式为UTF-8
        response.setContentType("text/html");  //设置响应正文的MIME类型
        PrintWriter out =response.getWriter();
        //返回一个PrintWriter对象，Servlet使用它来输出字符串形式的正文数据
        //以下为输出的HTML正文数据
        out.println("<HTML>");
        out.println("<HEAD><TITLE>ServletConfig</TITLE></HEAD>"); out.println("<BODY>");
        out.println("获取InitParamServlet的初始化参数\"encoding\"的字符串值:"+charset);
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        doPost(request,response);
    }
}
